﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TTGameEngine;

namespace AliveCard {

/// <summary>
/// 游戏触发器
///
/// 1.设置触发 Trigger
/// 2.事件触发 Event
/// 3.检测条件 Condition
/// 4.执行行为 Action
///
///
/// todo: 优化：目前单次触发结束后，只是做了Enable处理，没有删除。
///
/// </summary>
public class TriggerManager : SingletonMono<TriggerManager> {

    public GameTriggerData[] debug_StartTriggers;
    public GameTriggerData[] debug_EnterRoomTriggers;
    public GameTriggerData[] debug_ExitRoomTriggers;
    public GameTriggerData[] debug_EnterBattleTriggers;
    public GameTriggerData[] debug_ExitBattleTriggers;

    //Enum_TriggerTypeID<Triggers>
    List<List<GameTrigger>> m_triggers = new List<List<GameTrigger>>();
    // <GameEventID, TriggerType>
    Dictionary<GameEventID, TriggerTypeID> eventID2TriggerTypeMap = new Dictionary<GameEventID, TriggerTypeID>();

// Use this for initialization
    void Awake() {
        init();

    }

// Update is called once per frame
    void Update() {

    }

    protected override void init() {
        LoadTestTriggers();
        initEventForTrigers();
    }

    public void LoadTestTriggers() {
        for(int i = 0; i < (int)TriggerTypeID.Count; i++) {
            List<GameTrigger> newArr = new List<GameTrigger>();
            m_triggers.Add(newArr);
        }
        foreach(GameTriggerData data in debug_StartTriggers) {
            m_triggers[(int)TriggerTypeID.GT_StartGame].Add(GameTrigger.NewWithData(data));
        }
        foreach(GameTriggerData data in debug_EnterRoomTriggers) {
            m_triggers[(int)TriggerTypeID.GT_EnterRoom].Add(GameTrigger.NewWithData(data));
        }
        foreach(GameTriggerData data in debug_ExitRoomTriggers) {
            m_triggers[(int)TriggerTypeID.GT_ExitRoom].Add(GameTrigger.NewWithData(data));
        }
        foreach(GameTriggerData data in debug_EnterBattleTriggers) {
            m_triggers[(int)TriggerTypeID.GT_EnterBattle].Add(GameTrigger.NewWithData(data));
        }
        foreach(GameTriggerData data in debug_ExitBattleTriggers) {
            m_triggers[(int)TriggerTypeID.GT_ExitBattle].Add(GameTrigger.NewWithData(data));
        }

    }

    public void initEventForTrigers() {
        RegistEventForTrigger(GameEventID.GE_StartGame, TriggerTypeID.GT_StartGame);
        RegistEventForTrigger(GameEventID.GE_EnterRoom, TriggerTypeID.GT_EnterRoom);
        RegistEventForTrigger(GameEventID.GE_ExitRoom, TriggerTypeID.GT_ExitRoom);
        RegistEventForTrigger(GameEventID.GE_EnterBattle, TriggerTypeID.GT_EnterBattle);
        RegistEventForTrigger(GameEventID.GE_ExitBattle, TriggerTypeID.GT_ExitBattle);
    }

    void RegistEventForTrigger(GameEventID eventID, TriggerTypeID triType) {
        TTEventSystem.Ins.RegistEventHandler(eventID, OnGTEvent);
        eventID2TriggerTypeMap.Add(eventID, triType);
    }

    TriggerTypeID eventID2TriggerType(GameEventID eventID) {
        return eventID2TriggerTypeMap[eventID];
    }

    public void OnGTEvent(GameEvent e) {
        TriggerTypeID triType = eventID2TriggerType(e.m_eventID);

        DebugUtil.LogFormat("[TriggerMgr] TriggerEvent raised...[{0}]", triType.ToString());

        List<GameTrigger> triggers = m_triggers[(int)triType];

        foreach(GameTrigger gt in triggers) {
            gt.SetArgs(e.m_args);
            handlerTrigger(gt);
        }
    }

    //处理单个触发器
    void handlerTrigger(GameTrigger gt) {
        if(gt.isEnable == false) return;
        if(checkTriggerCondition(gt)) {
            DebugUtil.LogFormat("[TriggerMgr] Trigger Action...[{0}]", gt.name);
            actionTrigger(gt);
        }
    }

    bool checkTriggerCondition(GameTrigger gt) {
        List<GTCondition> conditions = gt.m_conditions;
        bool result = true;
        foreach(GTCondition con in conditions) {
            if (con.IsConditionOK(gt.m_args) == false)
            {
                result = false;
                break;
            }
        }
        return result;
    }

    void actionTrigger(GameTrigger gt) {
        if(gt.m_isOnlyOnce) gt.isEnable = false;
        List<GTAction> conditions = gt.m_actions;
        foreach(GTAction act in conditions) {
            act.DoIt();
        }
    }
}
}